package com.ruoyi.web.controller.file_info;

import java.awt.*;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.file_info.domain.CompetitionFileInfo;
import com.ruoyi.file_info.service.ICompetitionFileInfoService;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件上传Controller
 * 
 * @author LBH
 * @date 2023-12-14
 */
@RestController
@RequestMapping("/file_info/info")
public class CompetitionFileInfoController extends BaseController {
    @Autowired
    private ICompetitionFileInfoService competitionFileInfoService;
    @Autowired
    com.ruoyi.common.config.RuoYiConfig RuoYiConfig;

    /**
     * 查询文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('file_info:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompetitionFileInfo competitionFileInfo) {
        startPage();
        List<CompetitionFileInfo> list = competitionFileInfoService.selectCompetitionFileInfoList(competitionFileInfo);
        return getDataTable(list);
    }

    /**
     * 导出文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('file_info:info:export')")
    @Log(title = "文件导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompetitionFileInfo competitionFileInfo) {
        List<CompetitionFileInfo> list = competitionFileInfoService.selectCompetitionFileInfoList(competitionFileInfo);
        ExcelUtil<CompetitionFileInfo> util = new ExcelUtil<CompetitionFileInfo>(CompetitionFileInfo.class);
        util.exportExcel(response, list, "文件上传数据");
    }

    /**
     * 获取文件上传详细信息
     */
    @PreAuthorize("@ss.hasPermi('file_info:info:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") String fileId) {
        return success(competitionFileInfoService.selectCompetitionFileInfoByFileId(fileId));
    }

    /**
     * 新增文件上传
     */
    @PreAuthorize("@ss.hasPermi('file_info:info:add')")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompetitionFileInfo competitionFileInfo) {
        return toAjax(competitionFileInfoService.insertCompetitionFileInfo(competitionFileInfo));
    }

    /**
     * 修改文件上传
     */
    @PreAuthorize("@ss.hasPermi('file_info:info:edit')")
    @Log(title = "文件修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompetitionFileInfo competitionFileInfo) {
        return toAjax(competitionFileInfoService.updateCompetitionFileInfo(competitionFileInfo));
    }

    /**
     * 删除文件上传
     */
    @PreAuthorize("@ss.hasPermi('file_info:info:remove')")
    @Log(title = "文件删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable String[] fileIds) {
        return toAjax(competitionFileInfoService.deleteCompetitionFileInfoByFileIds(fileIds));
    }


    @PreAuthorize("@ss.hasPermi('file_info:info:download')")
    @Log(title = "文件下载", businessType = BusinessType.UPDATE)
    @PostMapping("/download")
    public void fileDownload(@RequestBody CompetitionFileInfo competitionFileInfoDTO, HttpServletResponse response, HttpServletRequest request) throws IOException {
        CompetitionFileInfo competitionFileInfo = competitionFileInfoService.selectCompetitionFileInfoByFileId(competitionFileInfoDTO.getFileId());

        response.setHeader("Content-Disposition", "attachment; filename="+"DownLoadFile");
        response.setContentType("application/octet-stream; charset=UTF-8");
        FileUtils.setAttachmentResponseHeader(response, competitionFileInfo.getFileName());
        System.out.println("downloadPath:"+competitionFileInfo.getFilePath());
        FileUtils.writeBytes(competitionFileInfo.getFilePath(), response.getOutputStream());
//        response.setContentType("application/zip");
//        response.setCharacterEncoding("utf-8");
//        response.setHeader("Content-Disposition", "attachment; filename=competition_system_file.zip");
//        String filePath = competitionFileInfo.getFilePath();
//        String fileSeparator = System.getProperty("os.name").startsWith("Windows") ? "\\" : "/";
//        String absolutePath = filePath.replace(fileSeparator, File.separator);
//        File file = new File(absolutePath);
//        try (ZipOutputStream zip = new ZipOutputStream(response.getOutputStream());
//             FileInputStream input = new FileInputStream(file)) {
//            ZipEntry zipEntry = new ZipEntry(competitionFileInfo.getFileName());
//            zip.putNextEntry(zipEntry);
//            IOUtils.copy(input, zip);
//            zip.finish();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}

